package org.lhz.service.impl;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSON;
import org.lhz.dao.SysMenuMapper;
import org.lhz.dao.impl.SysMenuMapperImpl;
import org.lhz.entity.SysMenu;
import org.lhz.service.SysMenuService;
import org.lhz.vo.SysMenuVo;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import utils.DataGridView;
import utils.JedisPoolUtils;
import utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class SysMenuServiceImpl implements SysMenuService {
    //注入dao层的对象
    private SysMenuMapper sysMenuMapper = new SysMenuMapperImpl();
    @Override
    public List<SysMenu> findAllMenuList(SysMenuVo sysMenuVo) {
        return sysMenuMapper.findAllMenuList(sysMenuVo);
    }

    @Override
    public List<SysMenu> findMenuByUserId(Integer userid) {
        return sysMenuMapper.findMenuByUserId(userid);
    }

    @Override
    public List<SysMenu> findAllMenu(SysMenuVo sysMenuVo) {
        return sysMenuMapper.findAllMenu(sysMenuVo);
    }

    @Override
    public int checkMenuById(Integer id) {
        return sysMenuMapper.checkMenuById(id);
    }
    /**
     * Infor: 删除菜单操作成功之后需要清空redis中的数据
     * @param id
     * @return : int
     * @author : LHZ
     * @date : 2021/11/5 11:11
     */
    @Override
    public int deleteMenuById(Integer id) {
        int count = sysMenuMapper.deleteMenuById(id);
        if (count >0) {
            JedisPoolUtils.getJedis().del("menu_list");
        }
        return count;
    }

    /**
     * Infor: 添加菜单之后需要删除redis中的数据
     * @param sysMenu
     * @return : int
     * @author : LHZ
     * @date : 2021/11/5 11:15
     */
    @Override
    public int addMenu(SysMenu sysMenu) {
        int count = sysMenuMapper.addMenu(sysMenu);
        if (count>0) {
            JedisPoolUtils.getJedis().del("menu_list");
        }
        return count;
    }

    /**
     * Infor: 更新菜单之后需要删除redis中的数据
     * @param sysMenu
     * @return : int
     * @author : LHZ
     * @date : 2021/11/5 11:17
     */
    @Override
    public int updateMenu(SysMenu sysMenu) {
        int count = sysMenuMapper.updateMenu(sysMenu);
        if (count >0) {
            JedisPoolUtils.getJedis().del("menu_list");
        }
        return count;
    }

    /**
     * Infor: 在更改角色菜单信息的情况下需要先清空该角色下的所有菜单
     *        信息
     * @param roleid
     * @param ids
     * @return : int
     * @author : LHZ
     * @date : 2021/11/1 11:13
     */
    @Override
    public boolean insertMenuByRoleId(Integer roleid,String ids) {
        try {
            //通过角色id删除该角色下的所有菜单信息
            sysMenuMapper.deleteMenuByRoleId(roleid);
            String[] menuIds = ids.split(",");
            System.out.println(menuIds);
            //循环插入菜单数据
            for (int i = 0; i < menuIds.length; i++) {
                sysMenuMapper.insertMenuByRoleId(roleid,Integer.parseInt(menuIds[i]));
            }
            return true;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Infor: 获取总数
     * @param
     * @return : java.lang.Long
     * @author : LHZ
     * @date : 2021/11/4 23:02
     */
    @Override
    public Long getTotal(SysMenuVo sysMenuVo) {
        return sysMenuMapper.getTotal(sysMenuVo);
    }

    /**
     * Infor:从redis中去获取数据
     * @param
     * @return : java.lang.String
     * @author : LHZ
     * @date : 2021/11/5 11:01
     */
    @Override
    public String getMenuListByRedis() {
        Jedis jedis = JedisPoolUtils.getJedis();
        String menu_list = jedis.get("menu_list");
        if (StringUtils.isEmpty(menu_list)) {
            List<SysMenu> allMenuList = sysMenuMapper.findAllMenuList(null);
            //创建一个TreeNode类型的集合存储
            ArrayList<TreeNode> list = new ArrayList<TreeNode>();
            for (SysMenu result : allMenuList) {
                //设置参数
                Integer id = result.getId();
                Integer pid = result.getPid();
                String title = result.getTitle();
                boolean spread = result.getSpread() == 1;
                //写进treenode集合中
                list.add(new TreeNode(id, pid,  title, spread));
            }
            DataGridView dataGridView = new DataGridView(list);

            menu_list = jedis.set("menu_list",JSON.toJSONString(dataGridView));
        }
        return menu_list;
    }

}
